package dataStructure;

import java.lang.*;
import java.io.*;

public class TreeNode {
        private int value;              //value of the node
        public TreeNode left;   //reference to the left child
        public TreeNode right; //reference to the right child
       
        public TreeNode(int inValue) {
                value = inValue;
                left = null;
                right = null;
        }
       
        public TreeNode() {
                value = 0;
        }
       
        public int getValue() {
                return value;
        }
       
        public void set(int inValue) {
                value = inValue;
        }
       
        public void forwardTravel() {
                if(left == null) {
                        p(value);
                        return ;
                }
                else if(right == null) {
                        p(value);
                        return ;
                }
               
                left.forwardTravel();
                p(value);
                right.forwardTravel();
        }
       
        public void middleTravel() {
                if(left == null) {
                        p(value);
                        return ;
                }
                else if(right == null) {
                        p(value);
                        return ;
                }
                p(value);
                left.forwardTravel();
                right.forwardTravel();
        }
       
        public void backwardTravel() {
                if(left == null) {
                        p(value);
                        return ;
                }
                else if(right == null) {
                        p(value);
                        return ;
                }
               
                left.forwardTravel();
                right.forwardTravel();
                p(value);
        }
       
        public void p(int a) {
                System.out.print(" "+a+" ");
        }
       
        public void p(String s) {
                System.out.println(s);
               
        }
       
        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                TreeNode root = new TreeNode(4);
               
                TreeNode lchild = new TreeNode(3);
                root.left = lchild;
               
                TreeNode rchild = new TreeNode(5);
                root.right = rchild;
               
                System.out.println(" forward travel : ");
                root.forwardTravel();
                System.out.println("\n middle travel : ");
                root.middleTravel();
                System.out.println("\n backward travel : ");
                root.backwardTravel();
               
        }

}


